package com.ruoyi.web.controller.web;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginBody;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.web.service.SysLoginService;
import com.ruoyi.framework.web.service.SysRegisterService;
import com.ruoyi.nongjt.domain.NjtUserRegister;
import com.ruoyi.nongjt.service.INjtUserRegisterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 登录验证Controller(前端Web)
 * 
 * @author ruoyi
 * @date 2024-09-27
 */
@RestController
@RequestMapping("/web")
public class WebLoginController
{
    @Autowired
    private SysLoginService loginService;

    @Autowired
    private SysRegisterService registerService;

    @Autowired
    private INjtUserRegisterService userRegisterService;

    /**
     * 登录方法
     * 
     * @param loginBody 登录信息
     * @return 结果
     */
    @PostMapping("/login")
    public AjaxResult login(@RequestBody LoginBody loginBody)
    {
        AjaxResult ajax = AjaxResult.success();
        // 生成令牌
        String token = loginService.login(loginBody.getUsername(), loginBody.getPassword(), loginBody.getCode(),
                loginBody.getUuid());
        ajax.put("token", token);
        return ajax;
    }

    /**
     * 退出登录
     */
    @PostMapping("/logout")
    public AjaxResult logout()
    {
        loginService.logout("0");
        return AjaxResult.success("退出成功");
    }

    /**
     * 注册方法
     */
    @PostMapping("/register")
    public AjaxResult register(@RequestBody NjtUserRegister user)
    {
        if (!StringUtils.isEmpty(user.getPassword()))
        {
            return AjaxResult.error("用户密码不允许自主设置");
        }
        String msg = registerService.register(user);
        return StringUtils.isEmpty(msg) ? AjaxResult.success("注册成功") : AjaxResult.error(msg);
    }
}